﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using EnvDTE;
using System.Reflection;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;

namespace CodeBuilder.Controls
{
    public class ExTextEdidor : ICSharpCode.TextEditor.TextEditorControl
    {
        #region 导出代码到解决方案
        public _DTE application = EnvConfig.ApplicationObject;

        public void InitEvnt()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception exception1)
            {
                MessageBox.Show("\u81ea\u52a8\u751f\u6210\u4ee3\u7801\u53d1\u751f\u5f02\u5e38,\u9519\u8bef\u4fe1\u606f:" + exception1.Message, "\u9519\u8bef\u63d0\u793a", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        #endregion

        private ContextMenuStrip contextMenu; //上下文菜单
        private ToolStripMenuItem itemSave; //导出文件
        private ToolStripMenuItem itemCodeCreate;   //代码生成
        private SaveFileDialog fileDialog;

        /// <summary>
        /// 编辑器类型
        /// </summary>
        public TextStyleType StyleType
        {
            get;
            set;
        }

        private string ConvertEditorHighlightingStrategy()
        {
            switch (StyleType)
            {
                case TextStyleType.cs:
                    return "C#";
                case TextStyleType.aspx:
                    return "ASP/XHTML";
                default:
                    return StyleType.ToString().ToUpper();
            }
        }

        private void InitControls()
        {
            application = (_DTE)EnvConfig.ApplicationObject;

            contextMenu = new ContextMenuStrip();

            itemSave = new ToolStripMenuItem();
            itemSave.Name = "itemSave";
            itemSave.Text = "导出文件";
            itemSave.Click += new EventHandler(itemSave_Click);

            itemCodeCreate = new ToolStripMenuItem();
            itemCodeCreate.Name = "itemCodeCreate";
            itemCodeCreate.Text = "导出开发环境";
            itemCodeCreate.Click += new EventHandler(itemCodeCreate_Click);

            this.contextMenu.Items.AddRange(new ToolStripItem[] { itemSave, itemCodeCreate });
            this.ContextMenuStrip = contextMenu;

            fileDialog = new SaveFileDialog();
            fileDialog.FileName = "";
            fileDialog.Filter = "C#文件|*.cs|SQL脚本|*.sql|VB文件|*.vb|Xml文件|*.xml|aspx|*.aspx|所有文件|*.*";

            this.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(ConvertEditorHighlightingStrategy());
        }

        public ExTextEdidor(TextStyleType type, string Code)
            : base()
        {
            this.StyleType = type;
            this.Text = Code;

            InitControls();
        }

        /// <summary>
        /// 导出到开发环境
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void itemCodeCreate_Click(object sender, EventArgs e)
        {
            this.InitEvnt();
            if (this.application == null)
            {
                MessageBox.Show("请先打开系统解决方案！");
                return;
            }
            if (this.application.ActiveDocument == null)
            {
                MessageBox.Show("请先打开要插入代码的C#或者aspx文件，并将光标放置在插入点！");
                return;
            }
            //获取生成的代码
            string sCode = this.Text;
            InsertCode(sCode);


        }

        /// <summary>
        /// 导出程序于开发环境中
        /// </summary>
        /// <param name="sCode"></param>
        private void InsertCode(string sCode)
        {
            
            if (this.application.ActiveDocument != null && (
            this.application.ActiveDocument.Language == "CSharp" || this.application.ActiveDocument.Language == "HTML"))//is TextDocument)
            {
                //LED 一定要转换，否则回出错的
                TextDocument doc = (TextDocument)this.application.ActiveDocument.Object("");
                doc.ClearBookmarks();
                EditPoint point = doc.CreateEditPoint(doc.Selection.ActivePoint);//.in.StartPoint);
                point.Insert(sCode);
            }
            else
            {
                MessageBox.Show("请确保打开的是C#或者aspx代码,并指定了代码插入点！");
            }
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void itemSave_Click(object sender, EventArgs e)
        {
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                this.SaveFile(fileDialog.FileName);
                MessageBox.Show("保存文件成功！");
            }
        }
    }
}
